package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"quiz-master/cache"
	"quiz-master/model"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_no := c.PostForm("car_no")
	car_name := c.PostForm("car_name")
	car_node := c.PostForm("car_node")
	if car_no == "" || car_name == "" || car_node == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Car{
		CarNo:   car_no,
		CarName: car_name,
		CarCode: car_node,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_from := c.PostForm("order_from")
	order_to := c.PostForm("order_to")
	order_good := c.PostForm("order_good")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if order_no == "" || order_from == "" || order_to == "" || order_good == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.CarOrder{
		OrderNo:   order_no,
		OrderFrom: order_from,
		OrderTo:   order_to,
		OrderGood: order_good,
		Cid:       cid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	/*
		SELECT
		 car_orders.id as OrderId,
		 cars.car_no as CarNo,
		 cars.car_name as CarName,
		 cars.car_code as CarCode,
		 car_orders.order_no as OrderNo,
		 car_orders.order_from as OrderFrom,
		 car_orders.order_to as OrderTo,
		 car_orders.order_good as OrderGood,
		FROM car_orders
		LEFT JOIN cars ON cars.id=car_orders.cid
	*/
	var list []model.CarOrderList
	redis_key := "redis_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"cars.car_code as CarCode",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
			).Joins("LEFT JOIN cars ON cars.id=car_orders.cid").
			Offset(offset).Limit(size).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	model.DB.Model(&model.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
			SELECT
				 car_orders.id as OrderId,
				 cars.car_no as CarNo,
				 cars.car_name as CarName,
				 cars.car_code as CarCode,
				 car_orders.order_no as OrderNo,
				 car_orders.order_from as OrderFrom,
				 car_orders.order_to as OrderTo,
				 car_orders.order_good as OrderGood
				FROM car_orders
				LEFT JOIN cars ON cars.id=car_orders.cid
		    WHERE car_orders.id =1

	*/
	var carorder model.CarOrderList
	redis_key := "redis_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"cars.car_code as CarCode",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
			).Joins("LEFT JOIN cars ON cars.id=car_orders.cid").
			Where("car_orders.id = ?", id).Find(&carorder)
		jsonData, _ := json.Marshal(carorder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carorder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": carorder,
	})
	return
}
